package com.mp.approvalreq.util;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.service.*;
import com.mp.user.entity.User;
import com.mp.user.service.DepartmentServicePrx;
import com.mp.user.service.UserServicePrx;
import com.plusmoney.exception.AuthenticationException;
import com.plusmoney.exception.ExceptionMessage;
import com.qq.tars.client.util.ServantUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;


/**
 * 服务工具类
 *
 * @author Administrator
 */
public class ServiceUtils {
    private static Logger logger = LoggerFactory.getLogger(ServiceUtils.class);

    public static UserServicePrx getUserServicePrx() {
        return ServantUtils.getObject(UserServicePrx.class);
    }


    public static ReadTemplateServicePrx getReadTemplateServicePrx() {
        return ServantUtils.getObject(ReadTemplateServicePrx.class);
    }

    public static SelectApprovalServicePrx getSelectApprovalServicePrx() {
        return ServantUtils.getObject(SelectApprovalServicePrx.class);
    }

    public static ApprovalOperateServicePrx getApprovalOperateServicePrx() {
        return ServantUtils.getObject(ApprovalOperateServicePrx.class);
    }

    public static SubmitRequestServicePrx getSubmitRequestServicePrx() {
        return ServantUtils.getObject(SubmitRequestServicePrx.class);
    }

    public static RedPointServicePrx getRedPointServicePrx() {
        return ServantUtils.getObject(RedPointServicePrx.class);
    }


    public static CommontApprovalServicePrx getCommontApprovalServicePrx() {
        return ServantUtils.getObject(CommontApprovalServicePrx.class);
    }

    static DepartmentServicePrx getDepartmentServicePrx() {
        return ServantUtils.getObject(DepartmentServicePrx.class);
    }

    public static User getUserInfo(HttpServletRequest req) throws Throwable {
        UserServicePrx userService = ServantUtils.getObject(UserServicePrx.class);
        String authHeader = req.getHeader("Authorization");
        logger.info(String.format("header Authorization : %s", authHeader));
        if (StringUtils.isNotEmpty(authHeader)) {
            int index = authHeader.indexOf("Bearer ");
            if (index >= 0) {
                String token;
                try {
                    token = authHeader.substring("Bearer ".length());
                } catch (Exception e) {
                    token = null;
                }
                if (StringUtils.isNotEmpty(token)) {
                    User user = userService.getUserInfoByToken(token);
                    simplify(user);
                    return user;
                }
            }
        }
        ExceptionMessage message = new ExceptionMessage();
        message.setRespCode("990");
        message.setRespDesc("用户未登录");
        throw new AuthenticationException(JSONObject.toJSONString(message));
    }

    private static void simplify(User user) {
        user.setActive(null);
        user.setEmail(null);
        user.setImageName(null);
        user.setInfoId(null);
        user.setIsIDirector(null);
        user.setMainContactId(null);
        user.setOS(null);
        user.setPhone(null);
        user.setPmFtoken(null);
        user.setPosition(null);
        user.setPositionCompany(null);
        user.setUserName(null);
        user.setVersion(null);
        user.setToken(null);
    }

    public static User getUserInfoByRequest(HttpServletRequest req) throws Throwable {
        String authHeader = req.getHeader("Authorization");
        logger.info(String.format("header Authorization : %s", authHeader));
        if (StringUtils.isNotEmpty(authHeader)) {
            int index = authHeader.indexOf("Bearer ");
            if (index >= 0) {
                String token;
                try {
                    token = authHeader.substring("Bearer ".length());
                } catch (Exception e) {
                    token = null;
                }
    /*
                   这个if-else是特殊逻辑,仅用于GetApprovalCost
                 */
                if ("ZTY0NjI1ODVmOTE4MzRiY2Y1ZTczYzM4NzE5ODZmZDE5MGFlMjYzOGVkZTVmNjdjYjlhZGU1MTc4NWQ4MWQ4Yw".equals(token)) {
                    User user = new User();
                    String companyId = req.getParameter("companyId");
                    user.setCompanyId(Integer.valueOf(companyId));
                    return user;
                }

                if (StringUtils.isNotEmpty(token)) {
                    UserServicePrx userService = getUserServicePrx();
                    User user = userService.getUserInfoByToken(token);
                    user.setToken(token);
                    return user;
                }
            }
        }
        ExceptionMessage message = new ExceptionMessage();
        message.setRespCode("990");
        message.setRespDesc("用户未登录");
        throw new AuthenticationException(JSONObject.toJSONString(message));
    }


}
